package com.yuanye.recruitCrawler.utils;

import java.util.Random;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/** 
 * @author yuanye 2018年8月9日 下午4:45:42
 */
public class SeleniumUtil {
	private static boolean isServer=true;
	
	static {
		String os = System.getProperty("os.name");
		if (os.startsWith("win") || os.startsWith("Win")) {
			isServer=false;
		} else {
			isServer=true;
		}
	}
	
	public static void sleep(long time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public static void sleep(long benchmarkTime,int randomTime) {
		Random random=new Random();
		try {
			Thread.sleep(benchmarkTime+random.nextInt(randomTime));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public static void slowdownInput(WebElement element,String content) {
		Random random=new Random();
		for(int i=0;i<content.length();i++) {
			try {
				Thread.sleep(200+random.nextInt(100));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			char c=content.charAt(i);
			element.sendKeys(String.valueOf(c));
		}
	}
	public static ChromeDriver getChromeDriver() {
		ChromeDriver driver=null;
		if(isServer) {
			ChromeOptions options = new ChromeOptions();
			options.addArguments("--no-sandbox","--headless","--disable-gpu","--disable-dev-shm-usage");
			driver = new ChromeDriver(options);
		}else {
			driver = new ChromeDriver();
		}
		return driver;
	}
}